﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using RateInfo = HRM.BusinessLogic.RateInfo;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmRateInfo : FormBase
    {
        DialogResult _dlgResult;
        int _RateID;

        //!RateClient _wcfRate;
        RateInfo _RateInfo;

        RoleRightInfo _ListRole;
        bool isChange;
        public frmRateInfo(int RateID, RoleRightInfo ListRole)
        {
            InitializeComponent();
            _ListRole = ListRole;
            _RateID = RateID;
            _RateInfo = new RateInfo();
            _dlgResult = DialogResult.Cancel;
            //
            Init_Control();
        }

        protected override void Init_Control()
        {
            //
            FormGlobals.TextBox_SetHandle(DataInputType.NumberData, txtRate_Medical, txtRate_Social, txtRate_Unemployed, txtRate_Remain);
            FormGlobals.TextBox_SetFormat(Globals.CS_DISPLAY_RATE_FORMAT, txtRate_Medical, txtRate_Social, txtRate_Unemployed, txtRate_Remain);
            FormGlobals.Control_SetRequired(txtRate_Medical, txtRate_Social, txtRate_Unemployed, txtRate_Remain);
            //
            //FormGlobals.Panel_SetFormField(pnlInfo, Rate_Controller.cs_Table_Name); //-tạm thời lỗi thiếu bảng App_DicDomain để lấy các cột require
            //
            this.KeyPreview = true;
            if (_RateID > 0)
            {
                _RateInfo = Rate_Controller.Instance().Rate_GetInfo(_RateID);
                Info2Form();
            }
        }

        private void Info2Form()
        {
            FormGlobals.Panel_FillData(pnlInfo, _RateInfo);
            mskRate_Month.Text = _RateInfo.Rate_Month.ToString(Globals.CS_DISPLAY_MONTHYEAR_FORMAT);
            //txtSocial.Text = _RateInfo.Rate_Social.ToString("#0.0#");
            //txtUnemployed.Text = _RateInfo.Rate_Unemployed.ToString("#0.0#");
            //txtMedical.Text = _RateInfo.Rate_Medical.ToString("#0.0#");
            //txtRemain.Text = _RateInfo.Rate_Remain.ToString("#0.0#");
            //txtDescription.Text = _RateInfo.Rate_Description;
            //mskRate_Month.Text = _RateInfo.Rate_Month.ToString("MM/yyyy");
            isChange = false;
        }
        private void form2Info()
        {
            FormGlobals.Panel_GetData(pnlInfo, _RateInfo);
            _RateInfo.Rate_Month = UISetting.GetDate(mskRate_Month.Text);
            //_RateInfo.Rate_Social = Convert.ToDecimal(txtSocial.Text);
            //_RateInfo.Rate_Medical = Convert.ToDecimal(txtMedical.Text);
            //_RateInfo.Rate_Unemployed = Convert.ToDecimal(txtUnemployed.Text);
            //_RateInfo.Rate_Remain = Convert.ToDecimal(txtRemain.Text);
            //_RateInfo.Rate_Description = txtDescription.Text;
            _RateInfo.Rate_Id = _RateID;

        }

        //bool CheckValid()
        //{          
        //    if (!CheckTextValid.IsDecimal(txtRate_Social.Text))
        //    {
        //        txtRate_Social.Focus();
        //        MessageBox.Show("Mức BH xã hội không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return false;
        //    }
        //    if (!CheckTextValid.IsDecimal(txtRate_Medical.Text))
        //    {
        //        txtRate_Medical.Focus();
        //        MessageBox.Show("Mức BH y tế không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return false;
        //    }

        //    if (!CheckTextValid.IsDecimal(txtRate_Unemployed.Text))
        //    {
        //        txtRate_Unemployed.Focus();
        //        MessageBox.Show("Mức BH thất nghiệp không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return false;
        //    }

        //    if (!CheckTextValid.IsDecimal(txtRate_Remain.Text))
        //    {
        //        txtRate_Remain.Focus();
        //        MessageBox.Show("Mức BH khác không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //        return false;
        //    }
        //    return true;

        //}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!_ListRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_RateInfo.Rate_Id == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _RateInfo.Rate_Id = 0;         
            txtRate_Medical.Text = "";
            txtRate_Social.Text = "";
            txtRate_Unemployed.Text = "";
            txtRate_Remain.Text = "";
            txtRate_Description.Text = "";
            mskRate_Month.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (!CheckValid())
            //{
            //    isChange = true;
            //    return;
            //}
            if (!FormGlobals.Panel_CheckError(pnlInfo))
            {
                isChange = true;
                return;
            }
            //

            form2Info();
            if (_RateInfo.Rate_Id > 0)//Update
            {
                if (!_ListRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
               
                //if (_wcfRate.Rate_Update(_RateInfo))
                if (Rate_Controller.Instance().Rate_Update(_RateInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else //insert
            {
                if (!_ListRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
               
                //_RateInfo.Rate_Id = _wcfRate.Rate_Insert(_RateInfo);
                _RateInfo.Rate_Id = Rate_Controller.Instance().Rate_Insert(_RateInfo);
                if (_RateInfo.Rate_Id > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    _dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void frmRateInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
            }
        }

        private void frmRateInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            //!_wcfRate.Close();
            this.DialogResult = _dlgResult;
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void txtRate_Leave(object sender, EventArgs e)
        {
          
        }
       
    }
}